package vcp.views;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JRadioButton;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import vcp.common.AbstractView;
import vcp.common.IView;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.CancelOrderForm;
import vcp.gui.forms.OrderOccasionalForm;
import vcp.gui.forms.OrderSubscriptionForm;
import vcp.gui.forms.OrderTemporaryForm;
import vcp.gui.forms.OrderTrackForm;

/**
 * Order guest view
 *
 */
public class OrderGuestView extends AbstractView implements ActionListener,Observer,IView {
	
	private VCPPanel widgetPanel;
	private VCPPanel buttonPanel;
	private VCPPanel formPanel;
	
	private OrderSubscriptionForm orderSubscriptionForm;
    private OrderTemporaryForm orderTemporaryForm;
    private OrderOccasionalForm orderOccasionalForm;
    private CancelOrderForm cancelOrderViewForm;
	private OrderTrackForm orderTrackViewForm;
    
    private JRadioButton occasionalButton;
	private JRadioButton SubscriptionButton;
	private JRadioButton PreOrderButton;
	
	GroupLayout groupLayout;
	
    public OrderGuestView(){
		orderSubscriptionForm = getOrderSubscriptionForm();
		orderOccasionalForm = getOrderOccasionalViewForm();
		orderTemporaryForm = getOrderTemporaryViewForm();
		cancelOrderViewForm = getCancelOrderViewForm();
		orderTrackViewForm = getOrderTrackViewForm();
    }
    
    public OrderSubscriptionForm getOrderSubscriptionForm() {
		if(orderSubscriptionForm==null)
			orderSubscriptionForm = new OrderSubscriptionForm();
    	return orderSubscriptionForm;
	}
    public OrderOccasionalForm getOrderOccasionalViewForm() {
		if(orderOccasionalForm==null)
			orderOccasionalForm = new OrderOccasionalForm();
    	return orderOccasionalForm;
	}
    public OrderTemporaryForm getOrderTemporaryViewForm() {
		if(orderTemporaryForm==null)
			orderTemporaryForm = new OrderTemporaryForm();
    	return orderTemporaryForm;
	}
    
    public CancelOrderForm getCancelOrderViewForm() {
		if(cancelOrderViewForm==null)
			cancelOrderViewForm= new CancelOrderForm();
    	return cancelOrderViewForm;
	}
    
    public OrderTrackForm getOrderTrackViewForm() {
		if(orderTrackViewForm==null)
			orderTrackViewForm= new OrderTrackForm();
    	return orderTrackViewForm;
	}
    
    
   
    /* draw and return radio panel */   
    public VCPPanel getButtonPanel(){
    	if(buttonPanel==null){
    		buttonPanel = new VCPPanel(new GridLayout(1, 1));
    		buttonPanel.setBackground(Color.white);
    		occasionalButton = new JRadioButton("Occasional");
    		occasionalButton.setBackground(Color.white);
    		occasionalButton.setMnemonic(KeyEvent.VK_R);
    		occasionalButton.setActionCommand("OO");
    		occasionalButton.setSelected(true);

    		//SubscriptionButton = new JRadioButton("Subscription");
    		//SubscriptionButton.setBackground(Color.white);
    		//SubscriptionButton.setMnemonic(KeyEvent.VK_S);
    		//SubscriptionButton.setActionCommand("OS");
    		//SubscriptionButton.setSelected(false);

    		PreOrderButton = new JRadioButton("Temporary");
    		PreOrderButton.setBackground(Color.white);
    		PreOrderButton.setMnemonic(KeyEvent.VK_P);
    		PreOrderButton.setActionCommand("OT");
    		PreOrderButton.setSelected(false);
            
    		/* widget action listener */
    		occasionalButton.addActionListener(this);
    		//SubscriptionButton.addActionListener(this);
    		PreOrderButton.addActionListener(this);
    		
    		ButtonGroup btngroup = new ButtonGroup();
    		btngroup.add(occasionalButton);
    		btngroup.add(SubscriptionButton);
    		btngroup.add(PreOrderButton);


    		buttonPanel.add(occasionalButton);
    		//buttonPanel.add(SubscriptionButton);
    		buttonPanel.add(PreOrderButton);
    	}
    	return buttonPanel;
    }
    
    
	@Override
	public void addController(Object obj) {		
		//transfer controller instance to right forms
		orderSubscriptionForm.addController(obj);
		orderOccasionalForm.addController(obj);
		orderTemporaryForm.addController(obj);
		cancelOrderViewForm.addController(obj);
		orderTrackViewForm.addController(obj);
	}

	


	public VCPPanel getWidgetPanel() {
		if(widgetPanel==null){
			widgetPanel = new VCPPanel();
			widgetPanel.setBackground(Color.white);
			groupLayout = new GroupLayout(widgetPanel);
			groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
					.addComponent(getButtonPanel(), GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
					.addComponent(getFormPanel(), GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
			);
			groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
					.addGroup(groupLayout.createSequentialGroup()
						.addComponent(getButtonPanel(), GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(getFormPanel(), GroupLayout.DEFAULT_SIZE, 300, GroupLayout.PREFERRED_SIZE))
			);
			widgetPanel.setLayout(groupLayout);
			
		}
		return widgetPanel;
	}

	public VCPPanel getFormPanel() {
		if(formPanel==null){
		   formPanel = new VCPPanel();
		   formPanel.setBackground(Color.white);
		   formPanel.add(orderOccasionalForm);
		}
		return formPanel;
	}

    /* self widget switches */
	@Override
	public void actionPerformed(ActionEvent e) {
		if ((e.getActionCommand()).equals("OO")){
			formPanel.loadPanel(orderOccasionalForm);			
		} else if ((e.getActionCommand()).equals("OT")) {			
			formPanel.loadPanel(orderTemporaryForm);
		}/* else if ((e.getActionCommand()).equals("OS")) {			
			formPanel.loadPanel(orderSubscriptionForm);
		}*/
	}
	
	@Override
	public void update(Observable o, Object arg) {
			
	}


}
